<template>
  <!-- 车辆管理 -->
  <div v-loading="isLoading" class="one">
    <!-- 筛选表单 -->
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="车辆编号">
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item label="应载重量">
        <el-select v-model="weight" placeholder="请选择应载重量">
          <el-option
            v-for="(item, index) in weightList"
            :key="index"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应载体积">
        <el-select v-model="volume" placeholder="请选择应载体积">
          <el-option
            v-for="(item, index) in volumeList"
            :key="index"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆类型">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item>
        <el-button type="danger" @click="onSubmit">搜索</el-button>
        <el-button @click="cel">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 车辆详情表格 -->
    <el-button type="danger" @click="addCarFn">新增车辆</el-button>
    <el-table :data="carData" style="width: 100%">
      <!-- el-table-column 列：通过prop绑定数据中的键 -->
      <!-- 列名：通过label来设置 -->
      <el-table-column type="index" :index="indexFn" label="序号" width="80" />
      <el-table-column prop="id" label="车型编号" width="200" />
      <el-table-column prop="name" label="车辆类型" width="150" />
      <el-table-column prop="num" label="车型数量" width="100" />
      <el-table-column prop="allowableLoad" label="应载重量(吨)" width="120" />
      <el-table-column
        prop="allowableVolume"
        label="应载体积(立方米)"
        width="150"
      />
      <el-table-column prop="measureLong" label="长(米)" width="100" />
      <el-table-column prop="measureWidth" label="宽(米)" width="100" />
      <el-table-column prop="measureHigh" label="高(米)" width="100" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button type="text" size="small" @click="editCarFn(row.id)">
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            style="color: red"
            @click="delCarFn(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 新增车型弹窗 -->
    <addCarVue ref="addcar" :show-dialog.sync="showDialog" />
  </div>
</template>
<script>
import { getCarListApi, getCarDetailsApi, delCarApi } from '@/api/car'

import addCarVue from './components/add-car.vue'

export default {
  components: {
    addCarVue
  },
  data() {
    return {
      isLoading: false,
      showDialog: false, // 控制弹窗
      // 表单
      form: {
        id: '', // 编号
        name: '', // 类型
        weight: '', // 重量
        volume: '' // 体积
      },
      // 重量
      weight: '',
      weightList: [
        { name: '<1.8（吨）', value: 'RANGE_LEVEL_1' },
        { name: '1.8-1.6（吨）', value: 'RANGE_LEVEL_2' },
        { name: '6-14（吨）', value: 'RANGE_LEVEL_3' },
        { name: '14-30（吨）', value: 'RANGE_LEVEL_4' },
        { name: '30-50（吨）', value: 'RANGE_LEVEL_5' },
        { name: '50-100（吨）', value: 'RANGE_LEVEL_6' },
        { name: '100>（吨）', value: 'RANGE_LEVEL_7' }
      ],

      // 体积
      volume: '',
      volumeList: [
        { name: '<3（m³）', value: 'RANGE_LEVEL_1' },
        { name: '3-5（m³）', value: 'RANGE_LEVEL_2' },
        { name: '5-10（m³）', value: 'RANGE_LEVEL_3' },
        { name: '10-15（m³）', value: 'RANGE_LEVEL_4' },
        { name: '15-30（m³）', value: 'RANGE_LEVEL_5' }
      ],

      carData: [], // 所有的车辆
      total: 0, // 总条数
      page: 1, // 当前页码
      pageSize: 5, // 每页的数据条数
      cardetails: {} // 点击编辑当前的那一条数据
    }
  },
  created() {
    // 请求车辆数组
    this.getCarList()
  },
  methods: {
    // 请求车辆数组
    async getCarList(id, weight, name, volume) {
      this.isLoading = true
      const res = await getCarListApi(
        this.page,
        this.pageSize,
        id,
        weight,
        name,
        volume
      )
      this.carData = res.data.data.items
      // console.log(res.data.data)
      this.total = Number(res.data.data.counts)
      this.isLoading = false
    },
    // 序号
    indexFn(index) {
      return index + 1 + (this.page - 1) * this.pageSize
    },
    // 每页条数变化了，切换了每页的条数
    handleSizeChange(val) {
      // 切换了每页几条数据，跳回到第一页
      this.page = 1
      this.pageSize = val
      // console.log(this.pageSize)
      this.getCarList()
      // console.log(`每页 ${val} 条`)
    },
    // 当前页变化，改变当前页触发执行
    handleCurrentChange(val) {
      this.page = val
      this.getCarList()
      // console.log(`当前页: ${val}`)
    },

    // 搜索按钮
    onSubmit() {
      this.getCarList(this.form.id, this.weight, this.form.name, this.volume)
    },
    // 重置
    cel() {
      this.form = {
        id: '', // 编号
        name: '' // 类型
      }
      this.weight = ''
      this.volume = ''

      this.getCarList()
    },

    // 新增车型
    async addCarFn() {
      this.showDialog = true
    },
    // 编辑车型
    async editCarFn(id) {
      const res = await getCarDetailsApi(id)
      this.$refs.addcar.formData = res.data.data
      this.showDialog = true
    },
    // 删除车辆
    delCarFn(id) {
      this.$confirm('此操作将永久删除，是否继续？', '温馨提示')
        .then(async () => {
          await delCarApi(id)
          this.$message.success('删除成功')
          // 重新渲染数据
          this.getCarList()
        })
        .catch(() => {
          console.log('点击了取消')
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.one {
  background-color: #f3f5f9;
}
// 车辆表单
.el-form {
  background-color: #fff;

  margin: 20px 30px;
  padding: 40px 20px 20px 0;

  float: left;
  .el-form-item {
    float: left;
    width: 360px;
  }
}

// 车辆表格

.el-table--enable-row-hover {
  width: 1266px !important;
  margin-left: 30px;
  // margin-top: 120px;
  padding-top: 20px;
  padding-right: 30px;
  padding-left: 30px;
  margin-right: 30px;
}
// 分页
.el-pagination {
  width: 1300px;
  margin-left: 30px;
  padding: 20px 0 20px 400px;
  background-color: rgb(255, 255, 255);
}
</style>
